package com.example.photo_album.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.photo_album.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
public class FileController {

    @Autowired
    IFileService fileService;

    //文件上传接口 @RequestParam("file_data") MultipartFile[]
    @PostMapping("/uploadFile")
    public JSONObject uploadFile(HttpServletRequest request){
        HashMap<String,Object> resultMap = new HashMap<>();
        List<MultipartFile> fls=((MultipartHttpServletRequest)request).getFiles("input-fa");
        try {
            fileService.saveImage(fls,request.getParameter("content"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    //图片删除按钮操作
    @RequestMapping("/deleteFileByUuidName")
    public void deleteFileByUuidName(String uuidName){


    }

    //提交文件以及详情 保存内容等
    @RequestMapping("/submitFile")
    public HashMap<String,Object> submitFileInfo(@RequestParam("subject") String subject,
                                     @RequestParam("content") String content,
                                     @RequestParam("urls") String urls){

        List list = Arrays.asList(urls);
        //获取参数 主题 描述 图片地址 其他（是否公开）等等

        System.out.println(list);
        HashMap<String,Object> result = new HashMap<String, Object>();
//        result.put("subject",subject);
//        result.put("content",content);
//        result.put("urls",urls);
        return result;
    }
}
